package mao.thinkinjava.io.serialize;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Random;

public abstract  class Shape implements Serializable {
    public static final int RED=0,GREEN=1,BLUE=2;
    public static int counter=0;
    private static Random random= new Random(47);
    private int xPos,yPos,dim;
    public Shape(int xPos,int yPos,int dim){
        this.xPos=xPos;
        this.yPos=yPos;
        this.dim=dim;
    }
    public  abstract int getColor();//static 和 abstract不能兼容，即static不能动态绑定。
    public abstract void setColor(int color);
    public abstract void serializeStaticField(ObjectOutputStream out) throws IOException;
    public abstract void recoverStaticField(ObjectInputStream in) throws IOException, ClassNotFoundException;
    @Override
    public String toString() {
        return getClass()+"Shape{" +"color"+getColor()+
                " xPos=" + xPos +
                ", yPos=" + yPos +
                ", dim=" + dim +
                '}'+"\n";
    }
    public static Shape randomFactory(){
        int xPos=random.nextInt(100);
        int yPos=random.nextInt(100);
        int dim=random.nextInt(100);
        switch ((counter++)%3){
            case 0:return new Circle(xPos,yPos,dim);
            case 1:return new Rectangle(xPos,yPos,dim);
            case 2:return new Line(xPos,yPos,dim);
            default:return new Circle(xPos,yPos,dim);
        }
    }
}
